
<?php

echo "<br />/************************\\<br />";
echo "&nbsp;Trying to test Exception<br />";
echo "\************************/<br />";

//create function with an exception
function checkNum($number)
{
    if ($number > 1)
    {
        throw new Exception("Value must be 1 or below");
    }
    return true;
}

//trigger exception in a "try" block
try
{
    checkNum(2);
    //If the exception is thrown, this text will not be shown
    echo 'If you see this, the number is 1 or below';
}

//catch exception
catch (Exception $e)
{
    echo 'Message: ' . $e->getMessage();
}
?> 

<?php

echo "<br />/******************************************\\<br />";
echo "&nbsp;Testing array operations with PHPUnit<br />";
echo "\******************************************/<br />";

class StackTest extends PHPUnit_Framework_TestCase
{

    public function testPushAndPop()
    {
        $stack = array();
        $this->assertEquals(0, count($stack));
        array_push($stack, 'foo');
        $this->assertEquals('foo', $stack[count($stack) - 1]);
        $this->assertEquals(1, count($stack));
        $this->assertEquals('foo', array_pop($stack));
        $this->assertEquals(0, count($stack));
    }

}

$test = new StackTest();
$test->testPushAndPop();
?>
<?php

echo "<br />/*****************************************************\\<br />";
echo "&nbsp;Using a data provider that returns an array of arrays<br />";
echo "\*****************************************************/<br />";

class DataTest extends PHPUnit_Framework_TestCase
{

    /**
     * @dataProvider provider
     */
    public function testAdd($a, $b, $c)
    {
        $this->assertEquals($c, $a + $b);
    }

    public function provider()
    {
        return array(
            array(0, 0, 0),
            array(0, 1, 1),
            array(1, 0, 1),
            array(1, 1, 3)
        );
    }

}

$dataTest = new DataTest;
try
{
    $provider = $dataTest->provider();
    foreach ($provider as $p)
    {
        $dataTest->testAdd($p[0], $p[1], $p[2]);
    }
}
catch (Exception $e)
{
    echo 'Message: ' . $e->getMessage();
}
?>

<?php

echo "<br />/*******************************************************************************\\<br />";
echo "&nbsp;Using the @expectedExceptionMessage and @expectedExceptionCode annotations<br />";
echo "\*******************************************************************************/<br />";

class ExceptionTest extends PHPUnit_Framework_TestCase
{

    /**
     * @expectedException        InvalidArgumentException
     * @expectedExceptionMessage Right Message
     */
    public function testExceptionHasRightMessage($a)
    {
        throw new InvalidArgumentException($a, 10);
    }

    /**
     * @expectedException     InvalidArgumentException
     * @expectedExceptionCode 20
     */
    public function testExceptionHasRightCode($a)
    {
        throw new InvalidArgumentException(1, $a);
    }

}

$dataTest = new ExceptionTest;
try
{
    $a = "sdfdasf";
    $dataTest->testExceptionHasRightMessage($a);
}
catch (Exception $e)
{
    echo 'Message: ' . $e->getMessage();
}
?>
<?php

echo "<br />/*******************************************************************************\\<br />";
echo "&nbsp;Testing the output of a function or method<br />";
echo "\*******************************************************************************/<br />";

class OutputTest extends PHPUnit_Framework_TestCase
{

    public function testExpectFooActualFoo()
    {
        $this->expectOutputString('foo');
        print 'foo';
    }

    public function testExpectBarActualBaz()
    {
        $this->expectOutputString('bar');
        print 'baz';
    }

}

$outputTest = new OutputTest;
try
{
    $outputTest->testExpectBarActualBaz();
}
catch (Exception $e)
{
    echo 'Message: ' . $e->getMessage();
}

class EmptyTest extends PHPUnit_Framework_TestCase
{

    public function testFailure()
    {
        $this->assertEmpty(array('foo'));
    }

}

$emptyTest = new EmptyTest;
try
{
    $emptyTest->testFailure();
}
catch (Exception $e)
{
    echo 'Message: ' . $e->getMessage();
}
?>

<?php

echo "<br />/*******************************************************************************\\<br />";
echo "&nbsp;Stubbing a method call to return a fixed value<br />";
echo "\*******************************************************************************/<br />";

class SomeClass
{

    public function doSomething()
    {
        return "foo";
    }
    public function doSomethings()
    {
        return "foo";
    }

}

class StubTest extends PHPUnit_Framework_TestCase
{

    public function testStub()
    {
        // Create a stub for the SomeClass class.
        $stub = $this->getMock('SomeClass');

        // Configure the stub.
        $stub->expects($this->any())
                ->method('doSomethings')
                ->will($this->returnArgument(0));

        // Calling $stub->doSomething() will now return
        // 'foo'.
        $this->assertEquals('foo', $stub->doSomethings("bar","foo"));
    }

}

$emptyTest = new StubTest;
try
{
    $emptyTest->testStub();
}
catch (Exception $e)
{
    echo 'Message: ' . $e->getMessage();
}
?>